package com.contactbook.web.client;

import java.util.Set;


import com.contactbook.web.client.dto.GroupDTO;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ListOfGroupsWidget extends View {

	private VerticalPanel content;
	
	private final static String TITLE = "List of your groups of contacts";
	
	public ListOfGroupsWidget(ContactBook contactBook) {
		super(contactBook);
	}
	
	@Override
	public VerticalPanel getContent() {
		if (contactBook.isLogged()) {
			clearContent();
			return buildView();
		} else
			contactBook.switchView(Constants.LoginViewName);
		return null;
	}

	@Override
	public String getTitle() {
		return TITLE;
	}
	
	private VerticalPanel buildView() {
		content = new VerticalPanel();
		
		getGroups();
		
		return content;
	}
	
	private void clearContent() {
		if (content != null)
			content.clear();
	}

	private void getGroups() {
		AsyncCallback<Set<GroupDTO>> callback = new AsyncCallback<Set<GroupDTO>>() {

			@SuppressWarnings("deprecation")
			@Override
			public void onFailure(Throwable arg0) {
				clearContent();
				content.add(new ErrorPanel());
			}

			@Override
			public void onSuccess(Set<GroupDTO> groups) {
				for (GroupDTO group : groups) {

					DecoratorPanel panelGroup = new DecoratorPanel();
					VerticalPanel panelGroupWidgets = new VerticalPanel();
					panelGroupWidgets.setWidth(Constants.SmallPanelWidgetsStandartWidth);
					panelGroupWidgets.setSpacing(3);
					
					HorizontalPanel panelGroupTitle = new HorizontalPanel();
					panelGroupTitle.setWidth(Constants.GroupNameHeaderPanelWidth);
					panelGroupTitle.setStyleName("gwt-label-panelGoupTitleHr");
					panelGroupTitle.setSpacing(3);
					
					Image groupIcon = new Image(Constants.IconGroupPath);
					groupIcon.setSize(Constants.IconSizeConstant, Constants.IconSizeConstant);
										
					Label groupName = new Label(group.getName());
					groupName.setStyleName("gwt-label-detailedGroupNameHeader");
					
					int nbContacts = group.getContacts().size();
					Label groupNbContact = new Label("  (" + String.valueOf(nbContacts) + " contact" + ((nbContacts<=1)?")":"s)"));
					groupNbContact.setStyleName("gwt-label-groupNbContacts");

					panelGroupTitle.add(groupIcon);
					panelGroupTitle.add(groupName);
					panelGroupTitle.add(groupNbContact);
					
					panelGroupTitle.setCellVerticalAlignment(groupIcon, HasVerticalAlignment.ALIGN_BOTTOM);
					panelGroupTitle.setCellVerticalAlignment(groupName, HasVerticalAlignment.ALIGN_BOTTOM);
					panelGroupTitle.setCellVerticalAlignment(groupNbContact, HasVerticalAlignment.ALIGN_BOTTOM);
					panelGroupTitle.setCellHorizontalAlignment(groupNbContact, HasHorizontalAlignment.ALIGN_RIGHT);
					/*
					*/
					
					FlexTable table = new FlexTable();
					table.setCellSpacing(3);
					
					Label labelNote = new Label("Description");
					labelNote.setHeight("12px");
					labelNote.setStyleName("gwt-label-labelnote");
					
					Label labelDescription = new Label();
					labelDescription.setStyleName("gwt-label-labeldescription");
					labelDescription.setText(group.getDescription());
					
					table.setWidget(0, 0, labelNote);
					table.setWidget(1, 0, labelDescription);

					panelGroupWidgets.add(panelGroupTitle);
					panelGroupWidgets.add(table);
					
					panelGroup.add(panelGroupWidgets);
					
					content.add(panelGroup);
				}
			}			
		};
		contactBook.getGroupService().getGroups(contactBook.getCurrentSession().getLogin(), contactBook.getCurrentSession().getPassword(), callback);
	}
}
